const ages = process.argv.slice(2)
param = {}
const error = {
   nocmd(cmds, cmd) {
      const s = []
      for (const k in cmds) {
         s.push(k + '\t' + (cmds[k].describe || '描述为空'))
      }
      console.log(`无效的命令："${cmd}"\n支持以下命令：\n${s.join('\n')}`);
   },
   nooption(ops, op) {
      const s = ops.map(a => (a.ab || a.name) + '\t' + (a.describe || '描述为空'))
      console.log(`无效的选项："${op}"\n支持以下选项：\n${s.join('\n')}`);
   },
   missingparam(bsd, i) {
      console.log(`缺少参数"${bsd[i].name}"作为第"${i + 1}"个参数\n${bsd[i].describe || '描述为空'}`);
   }
}
function analy(template) {
   let opt, tem = template, cmds = tem.subcmd, bsdi = 0
   const max = ages.length - 1
   for (let i = 0; i <= max; i++) {
      const e = ages[i]
      if (tem.subcmd) {
         if (tem.subcmd[e]) {
            tem = tem.subcmd[e]
            cmds = tem.subcmd
         } else {
            error.nocmd(cmds, e)
            return false
         }
      } else {
         if (e[0] === '-') {
            let op, k
            if (e[1] === '-') {
               if (e === '---') {
                  if (opt) {
                     param[opt] = true
                     opt = null
                  }
                  continue
               }
               k = e.slice(2)
               op = tem.option.find(a => a.name === k)
            } else {
               k = e.slice(1)
               if (k.length !== 1) {
                  for (const v of k) {
                     const op = tem.option.find(a => a.ab === v)
                     if (op) {
                        param[op.name] = true
                     } else {
                        error.nooption(tem.option, k)
                        return false
                     }
                  }
                  continue
               }
               op = tem.option.find(a => a.ab === k)
            }
            if (i === max) {
               if (op) {
                  param[op.name] = true
               } else {
                  error.nooption(tem.option, k)
                  return false
               }
            }
            if (opt) {
               param[opt.name] = true
            }
            if (op) {
               opt = op
            } else {
               error.nooption(tem.option, k)
               return false
            }
         } else {
            if (opt) {
               if (opt.more) {
                  if (param[opt.name]) {
                     param[opt.name].push(e)
                  } else {
                     param[opt.name] = [e]
                  }
               } else {
                  param[opt.name] = e
               }
            } else {
               const bsd = tem.bsd[bsdi]
               if (bsd) {
                  if (bsd.more) {
                     if (param[bsd.name]) {
                        param[bsd.name].push(e)
                     } else {
                        param[bsd.name] = [e]
                     }
                  } else {
                     param[bsd.name] = e
                     bsdi++
                  }
               }
            }
         }
      }
   }
   if (tem.bsd[bsdi]) {
      if (bsdi < tem.bsd.length - 1 || !param[tem.bsd[bsdi].name]) {
         error.missingparam(tem.bsd, bsdi)
         return false
      }
   }
   if (tem.option) {
      for (const v of tem.option) {
         if (v.default !== undefined && param[v.name] === undefined)
            param[v.name] = v.default
      }
   }
   return tem
}
function cliParam(tem, init) {
   const c = analy(tem)
   if (c) {
      init && init()
      c.handle && c.handle(param)
   }
}
cliParam.error = error
module.exports = cliParam